from __future__ import absolute_import, unicode_literals
from basics import models as basics_models
import gevent
import time
from celery import shared_task
from utils.base_response import BaseResponse
from ProjectTool.AutomatedTool import automated_tool


#
@shared_task
def test(*args, **kwargs):
    print(args)
    print(kwargs)
    # query_set = basics_models.Cases.objects.filter().values('id','name')
    # print(query_set)

    # return dict(query_set)
    return {"1": 2}


def add(a, b):
    time.sleep(1)
    return a + b


@shared_task
def test_async(*args, **kwargs):
    b = time.time()
    p = []
    for i in range(6):
        p.append(gevent.spawn(add, i, i))
    gevent.joinall(p)
    e = time.time()
    return {"tm":'async cost time: %s' % (e - b)}


@shared_task
def test_sync(*args, **kwargs):
    b = time.time()
    for i in range(6):
        add(i, i)
    e = time.time()
    return {"tm": 'sync cost time: %s' % (e - b)}


# 测试用例执行的函数
@shared_task
def test_cases_func(*args, **kwargs):
    res = BaseResponse()
    try:
        res = automated_tool.test_cases(2, **kwargs)
    except Exception as e:
        res.status = 1
        res.msg = '任务失败！'
        res.error = str(e.args)
    return res.dict

# def get_data():
#     import json
#     with open(r"/data/data.json") as f:
#         return json.load(f)
